package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.commondata.bag.Item;
import com.motu.monstercity.server.game.commondata.build.*;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class BuildBaseData {
    private static Logger logger = LoggerFactory.getLogger(BuildBaseData.class);
    private static ConcurrentHashMap<Integer, BuildMain> buildMainMap = new ConcurrentHashMap<>(); // 建筑配置表
    private static ConcurrentHashMap<Integer, BuildUpgrade> buildUpgradeMap = new ConcurrentHashMap<>(); // 建筑升级配置表
    private static ConcurrentHashMap<Integer, BuildRecruit> buildRecruitMap = new ConcurrentHashMap<>(); // 建筑投资配置表
    private static ConcurrentHashMap<Integer, BuildBank> buildBankMap = new ConcurrentHashMap<>(); // 金库等级配置表
    private static ConcurrentHashMap<Integer, BuildTile> buildTileMap = new ConcurrentHashMap<>(); // 地图方块标
    private static ConcurrentHashMap<Long, BuildInitCoord> buildInitCoordMap = new ConcurrentHashMap<>(); // 初始城市建筑布局配置表
    private static ConcurrentHashMap<Integer, BuildHospital> buildHospitalMap = new ConcurrentHashMap<>(); // 医院等级配置表


    private static int maxRecruitNum = 0;// 最大招募次数


    // 公共数据初始化
    public static void init(List<Future> futures) {
        futures.add(initBuildMain());
        futures.add(initBuildUpgrade());
        futures.add(initBuildRecruit());
        futures.add(initBuildBank());
        futures.add(initBuildTile());
        futures.add(initBuildInitCoord());
        futures.add(initBuildHospital());
    }

    /**
     * 初始化建筑配置表
     */
    public static Future<Void> initBuildMain() {
        String table = "build_main";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, BuildMain> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        BuildMain entity = new BuildMain(result.getJsonObject(i));
                        tmp.put(entity.getBuildId(), entity);
                    }
                    buildMainMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static BuildMain getBuildMain(int id) {
        BuildMain result = buildMainMap.get(id);
        if (result == null) {
            logger.error("getBuildMain error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, BuildMain> getBuildMainMap() { return buildMainMap; }


    /**
     * 初始化建筑升级配置表
     */
    public static Future<Void> initBuildUpgrade() {
        String table = "build_upgrade";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, BuildUpgrade> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        BuildUpgrade entity = new BuildUpgrade(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    buildUpgradeMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static BuildUpgrade getBuildUpgrade(int id) {
        BuildUpgrade result = buildUpgradeMap.get(id);
        if (result == null) {
            logger.error("getBuildUpgrade error key = " + id);
        }
        return result;
    }

    public static BuildUpgrade getBuildUpgrade(int buildId, int level) {
        for (BuildUpgrade buildUpgrade : getBuildUpgradeMap().values()) {
            if (buildUpgrade.getBuildId() == buildId && buildUpgrade.getLevel() == level) {
                return  buildUpgrade;
            }
        }
        return null;
    }
    public static ConcurrentHashMap<Integer, BuildUpgrade> getBuildUpgradeMap() { return buildUpgradeMap; }

    /**
     * 初始化建筑投资配置表
     */
    public static Future<Void> initBuildRecruit() {
        String table = "build_recruit";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, BuildRecruit> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        BuildRecruit entity = new BuildRecruit(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                        if (entity.getId() > maxRecruitNum) {
                            maxRecruitNum = entity.getId();// 最大招募次数
                        }
                    }
                    buildRecruitMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static int getMaxRecruitNum() {
        return maxRecruitNum;
    }

    public static BuildRecruit getBuildRecruit(int id) {
        BuildRecruit result = buildRecruitMap.get(id);
        if (result == null) {
            logger.error("getBuildRecruit error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, BuildRecruit> getBuildRecruitMap() { return buildRecruitMap; }

    /**
     * 初始化金库等级配置表
     */
    public static Future<Void> initBuildBank() {
        String table = "build_bank";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, BuildBank> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        BuildBank entity = new BuildBank(result.getJsonObject(i));
                        tmp.put(entity.getLevel(), entity);
                    }
                    buildBankMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static BuildBank getBuildBank(int id) {
        BuildBank result = buildBankMap.get(id);
        if (result == null) {
            logger.error("getBuildBank error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, BuildBank> getBuildBankMap() { return buildBankMap; }

    /**
     * 初始化地图方块标
     */
    public static Future<Void> initBuildTile() {
        String table = "build_tile";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, BuildTile> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        BuildTile entity = new BuildTile(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                    }
                    buildTileMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static BuildTile getBuildTile(int id) {
        BuildTile result = buildTileMap.get(id);
        if (result == null) {
            logger.error("getBuildTile error key = " + id);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, BuildTile> getBuildTileMap() { return buildTileMap; }

    /**
     * 初始化初始城市建筑布局配置表
     */
    public static Future<Void> initBuildInitCoord() {
        String table = "build_init_coord";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Long, BuildInitCoord> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        BuildInitCoord entity = new BuildInitCoord(result.getJsonObject(i));
                        tmp.put(entity.getCrood(), entity);
                    }
                    buildInitCoordMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static BuildInitCoord getBuildInitCoord(long coord) {
        BuildInitCoord result = buildInitCoordMap.get(coord);
        if (result == null) {
            logger.error("getBuildInitCoord error key = " + coord);
        }
        return result;
    }
    public static ConcurrentHashMap<Long, BuildInitCoord> getBuildInitCoordMap() { return buildInitCoordMap; }

    /**
     * 初始化医院等级配置表
     */
    public static Future<Void> initBuildHospital() {
        String table = "build_hospital";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, BuildHospital> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        BuildHospital entity = new BuildHospital(result.getJsonObject(i));
                        tmp.put(entity.getType(), entity);
                    }
                    buildHospitalMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static BuildHospital getBuildHospital(int type) {
        BuildHospital result = buildHospitalMap.get(type);
        if (result == null) {
            logger.error("getBuildHospital error key = " + type);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, BuildHospital> getBuildHospitalMap() { return buildHospitalMap; }
}
